package com.dyzx.hbb.modules.fcxs.controller;

import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.modules.fcxs.entity.FcxsOrder;
import com.dyzx.hbb.modules.fcxs.entity.FcxsPaymentPlan;
import com.dyzx.hbb.modules.fcxs.entity.FcxsSchedule;
import com.dyzx.hbb.modules.fcxs.query.FcxsScheduleQuery;
import com.dyzx.hbb.modules.fcxs.service.FcxsOrderService;
import com.dyzx.hbb.modules.fcxs.service.FcxsScheduleService;
import com.dyzx.hbb.modules.fcxs.vo.FcxsScheduleVO;
import com.dyzx.hbb.modules.fcxs.vo.FcxsScheduleUVO;
import com.dyzx.hbb.common.controller.BaseController;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 进度管理控制器
 * 
 * 功能说明：负责处理房地产销售系统中订单进度的创建、查询、更新和删除等操作
 * 主要职责：
 * - 提供订单进度的CRUD接口
 * - 支持按条件查询订单进度列表
 * - 处理进度添加和编辑时的订单关联验证
 * - 支持按审批状态和进度状态筛选数据
 * - 关联展示订单信息和操作人信息
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "05.进度管理", description = "进度管理相关接口")
@RestController
@RequestMapping("/fcxs/schedule")
public class FcxsScheduleController extends BaseValidController<
    FcxsScheduleService,
    FcxsSchedule,
    FcxsScheduleQuery,
        FcxsScheduleVO,
    FcxsScheduleUVO
> {
    @Autowired
    private FcxsScheduleService fcxsScheduleService;
    @Autowired
    private FcxsOrderService fcxsOrderService;
    @Override
    protected FcxsScheduleService getBaseService() {
        return fcxsScheduleService;
    }

    @Override
    protected MyLambdaQueryWrapper<FcxsSchedule> queryWrapper(FcxsScheduleQuery query) {
        MyLambdaQueryWrapper<FcxsSchedule> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsSchedule::getId)
                .fcxsOrderNo(FcxsSchedule::getOrderId).as("order_no")
                .select(FcxsSchedule::getResult)
                .select(FcxsSchedule::getApprovalStatus)
                .select(FcxsSchedule::getStatus)
                .select(FcxsSchedule::getRemark)
                .select(FcxsSchedule::getCreateDate)
                .select(FcxsSchedule::getUpdateDate)
                .organName(FcxsSchedule::getOrganId).as("organ_name")
                .userName(FcxsSchedule::getOperatorId).as("operator_name");
        if (query != null){
            queryWrapper
                    .ge(query.getApprovalStatus()!=null, FcxsSchedule::getApprovalStatus, query.getApprovalStatus())

                    .eq(query.getStatus()!=null, FcxsSchedule::getStatus, query.getStatus());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<FcxsSchedule> queryInfoWrapper() {
        MyLambdaQueryWrapper<FcxsSchedule> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(FcxsSchedule::getId)
                .fcxsOrderNo(FcxsSchedule::getOrderId).as("order_no")
                .select(FcxsSchedule::getResult)
                .select(FcxsSchedule::getApprovalStatus)
                .select(FcxsSchedule::getStatus)
                .select(FcxsSchedule::getRemark)
                .select(FcxsSchedule::getCreateDate)
                .select(FcxsSchedule::getUpdateDate)
                .organName(FcxsSchedule::getOrganId).as("organ_name")
                .userName(FcxsSchedule::getOperatorId).as("operator_name");
        return queryWrapper;
    }

    @Override
    protected FcxsSchedule addValid(FcxsScheduleVO entity) {
        FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderIdOrOrderNo(entity.getOrderId(), entity.getOrderNo());
        if(fcxsOrder == null){
            throw new RuntimeException("找不到订单");
        }
        FcxsSchedule data = new FcxsSchedule();
        BeanUtil.copyProperties(entity, data);
        data.setOrderId(fcxsOrder.getId());
        System.out.println(data);
        return data;
    }

    @Override
    protected FcxsSchedule editValid(FcxsScheduleUVO entity) {
        if (entity.getOrderId() != null) {
            FcxsSchedule data = new FcxsSchedule();
            FcxsOrder fcxsOrder = fcxsOrderService.fondByOrderId(entity.getOrderId());
            if (fcxsOrder == null) {
                throw new RuntimeException("找不到订单");
            }

            BeanUtil.copyProperties(entity, data);
            data.setOrderId(fcxsOrder.getId());
            return data;
        } else {
            return null;
        }
    }
}
